package com.studymaster.util;

import java.sql.*;

import java.lang.String;

public class conn  {
	
	private  String URL="jdbc:mysql://localhost:3306/xb?useUnicode=true&characterEncoding=UTF-8";
	private  String user = "root"; 
	private  String password = "123456";
	private  String forname="com.mysql.jdbc.Driver";
	/////////////////////////////////////////////////////////
	

	public conn() {}
	public void closeConn(Connection conn)	{
		try {
			if (conn != null){
				conn.close();conn=null;
			} 
		}
		catch (Exception localException) {
			System.out.println(localException); 
		} 
	}
	public void closeRs(ResultSet rs){
		try {
			if (rs != null) {
				rs.close(); 
				rs = null; 
			} 
		}
		catch (Exception localException) {
			System.out.println(localException);
		} 
	}
	public void closeSt(Statement stmt)	{
		try {
			if (stmt != null) {
				stmt.close(); 
				stmt = null; 
			} 
		}
		catch (Exception localException) {
			
		} 
	}
//	public Connection getConn() throws ClassNotFoundException, SQLException {
//		Connection conn=null;
//		try {
//			Context env = (Context) new InitialContext().lookup("java:comp/env");
//			DataSource source = (DataSource) env.lookup("jdbc/teacher");
//			conn = source.getConnection();
//		}
//		catch(Exception ex) {ex.printStackTrace();}
//		return conn;
//	}
	public Connection getConn() throws ClassNotFoundException, SQLException
	{
		Connection conn=null;
		Class.forName(this.forname); 
		conn = DriverManager.getConnection(this.URL,this.user,this.password); 
		return conn;
	}
	public ResultSet getSelect(Connection conn,String sql){
		ResultSet rs = null;
		try{
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql); 
		} catch (Exception localException) {
			System.out.println(localException); 
		}
		return rs; 
	}

	public int getUpdate(Connection conn,String sql) throws SQLException{
		int i = -1;
		try {
			conn.setAutoCommit(false);
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			i = stmt.executeUpdate(sql); 
		} 
		catch (Exception localException) {
			conn.rollback();
			System.out.println(localException); 
		}
			conn.commit();
		return i;
	}
}
